
package com.epfresh.supplier.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.OrderTrackWord;

import java.util.Map;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;

public class OrderTrackViewBinder
        extends ItemViewBinder<OrderTrackWord, OrderTrackViewBinder.ViewHolder> implements View.OnClickListener {


    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;


    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_order_track, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull OrderTrackWord post) {

        holder.setData(post);
    }


    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);


            switch (v.getId()) {
                case R.id.tv_product_new:

                    break;

                default:
                    if (onRecyclerViewItemClickListener != null && o instanceof Map) {
                        onRecyclerViewItemClickListener.onItemClick(v, position, o);
                    }
                    break;
            }


        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        View vLineTop;
        View vLineBottom;
        View vLineFlag;
        View vContent;
        TextView tvWord;
        TextView tvTime;
        ImageView iv1;
        ImageView iv2;
        ImageView iv3;

        ViewHolder(@NonNull View itemView) {
            super(itemView);

            vContent = itemView.findViewById(R.id.ll_content);

            vLineTop = itemView.findViewById(R.id.v_line_top);
            vLineBottom = itemView.findViewById(R.id.v_line_bottom);
            vLineFlag = itemView.findViewById(R.id.v_flag);

            tvWord = itemView.findViewById(R.id.tv_word);
            tvTime = itemView.findViewById(R.id.tv_time);

            iv1 = itemView.findViewById(R.id.iv_image_1);
            iv2 = itemView.findViewById(R.id.iv_image_2);
            iv3 = itemView.findViewById(R.id.iv_image_3);
        }


        void setData(OrderTrackWord post) {


            if (!post.isTopLine()) {
                vLineTop.setVisibility(View.INVISIBLE);
            } else {
                vLineTop.setVisibility(View.VISIBLE);
            }

            if (!post.isBottomLine()) {
                vLineBottom.setVisibility(View.INVISIBLE);
            } else {
                vLineBottom.setVisibility(View.VISIBLE);
            }

            if (post.isTop()){
                tvWord.setTextColor(0xffff0000);
                vLineFlag.setBackgroundResource(R.drawable.order_track_top_flag);
                vLineBottom.setBackgroundColor(0xffff0000);
            }else {
                vLineFlag.setBackgroundResource(R.drawable.order_track_other_flag);
                vLineBottom.setBackgroundColor(0xffdcdcdc);
                tvWord.setTextColor(0xff898989);
            }

            if (post.isBoldTop()){
                vLineTop.setBackgroundColor(0xffff0000);
            }else {
                vLineTop.setBackgroundColor(0xffdcdcdc);
            }

            String[] affImgUrls = post.getAffImgUrls();

            if (affImgUrls != null){
                int len = affImgUrls.length;
                if (len == 1){
                    updateImageView(iv1,affImgUrls[0],false);
                }else if (len == 2){
                    updateImageView(iv1,affImgUrls[0],false);
                    updateImageView(iv2,affImgUrls[1],true);
                }else if (len == 3){
                    updateImageView(iv1,affImgUrls[0],false);
                    updateImageView(iv2,affImgUrls[1],true);
                    updateImageView(iv3,affImgUrls[2],false);
                }else {
                    vContent.setVisibility(View.GONE);
                }

            }else {
                vContent.setVisibility(View.GONE);
            }
            tvWord.setText(post.getWords());
            tvTime.setText(post.getTime());

        }


        public void updateImageView(ImageView iv,String url,boolean isMargin){
            vContent.setVisibility(View.VISIBLE);
            iv.setVisibility(View.VISIBLE);

            int w = LocalDisplay.SCREEN_WIDTH_PIXELS - LocalDisplay.dp2px(75);
            w /=3;
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(w,w);

            if (isMargin){
                lp.rightMargin = LocalDisplay.dp2px(5);
                lp.leftMargin = LocalDisplay.dp2px(5);
            }
            iv.setLayoutParams(lp);
            ImageLoader.getInstance().displayImage(url,iv);
        }

    }
}
